package com.chenyaowei.web.controller.product.attr;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.chenyaowei.common.annotation.Log;
import com.chenyaowei.common.core.controller.BaseController;
import com.chenyaowei.common.core.domain.AjaxResult;
import com.chenyaowei.common.enums.BusinessType;
import com.chenyaowei.product.domain.CywProductAttr;
import com.chenyaowei.product.service.ICywProductAttrService;
import com.chenyaowei.common.utils.poi.ExcelUtil;
import com.chenyaowei.common.core.page.TableDataInfo;

/**
 * 商品属性Controller
 *
 * @author 陈耀威
 * @date 2023-11-22
 */
@RestController
@RequestMapping("/product/attr")
public class CywProductAttrController extends BaseController
{
    @Autowired
    private ICywProductAttrService cywProductAttrService;

    @GetMapping("/list/base/{spuId}")
    public AjaxResult selectBaseAttrListBySpuId(@PathVariable("spuId") Long spuId){
        List<CywProductAttr> list = cywProductAttrService.selectBaseAttrListBySpuId(spuId);
        return success(list);
    }

    @GetMapping("/list/sale/{skuId}")
    public AjaxResult selectSaleAttrListBySpuId(@PathVariable("skuId") Long skuId){
        List<CywProductAttr> list = cywProductAttrService.selectSaleAttrListBySkuId(skuId);
        return success(list);
    }

    /**
     * 根据分类id获取属性
     * @param catelogId
     * @return
     */
    @GetMapping("/list/{catalogId}")
    public AjaxResult selectAttrListByCatelogId(@PathVariable("catalogId") Long catalogId){
        List<CywProductAttr> list = cywProductAttrService.selectAttrListByCatelogId(catalogId);
        Map<String, List<CywProductAttr>> map = new HashMap<>();
        map.put("saleAttr", list.stream().filter(item -> item.getAttrType() == 0L).collect(Collectors.toList()));
        map.put("baseAttr", list.stream().filter(item -> item.getAttrType() == 1L).collect(Collectors.toList()));
        return success(map);
    }

    /**
     * 查询商品属性列表
     */
    @PreAuthorize("@ss.hasPermi('product:attr:list')")
    @GetMapping("/list")
    public TableDataInfo list(CywProductAttr cywProductAttr)
    {
        startPage();
        List<CywProductAttr> list = cywProductAttrService.selectCywProductAttrList(cywProductAttr);
        return getDataTable(list);
    }

    /**
     * 导出商品属性列表
     */
    @PreAuthorize("@ss.hasPermi('product:attr:export')")
    @Log(title = "商品属性", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CywProductAttr cywProductAttr)
    {
        List<CywProductAttr> list = cywProductAttrService.selectCywProductAttrList(cywProductAttr);
        ExcelUtil<CywProductAttr> util = new ExcelUtil<CywProductAttr>(CywProductAttr.class);
        util.exportExcel(response, list, "商品属性数据");
    }

    /**
     * 获取商品属性详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:attr:query')")
    @GetMapping(value = "/{attrId}")
    public AjaxResult getInfo(@PathVariable("attrId") Long attrId)
    {
        return success(cywProductAttrService.selectCywProductAttrByAttrId(attrId));
    }

    /**
     * 新增商品属性
     */
    @PreAuthorize("@ss.hasPermi('product:attr:add')")
    @Log(title = "商品属性", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CywProductAttr cywProductAttr)
    {
        return toAjax(cywProductAttrService.insertCywProductAttr(cywProductAttr));
    }

    /**
     * 修改商品属性
     */
    @PreAuthorize("@ss.hasPermi('product:attr:edit')")
    @Log(title = "商品属性", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CywProductAttr cywProductAttr)
    {
        return toAjax(cywProductAttrService.updateCywProductAttr(cywProductAttr));
    }

    /**
     * 删除商品属性
     */
    @PreAuthorize("@ss.hasPermi('product:attr:remove')")
    @Log(title = "商品属性", businessType = BusinessType.DELETE)
	@DeleteMapping("/{attrIds}")
    public AjaxResult remove(@PathVariable Long[] attrIds)
    {
        return toAjax(cywProductAttrService.deleteCywProductAttrByAttrIds(attrIds));
    }
}
